﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Xml;

/// <summary>
/// Array of ticket types available for this event
/// </summary>
public class Tickets:List<Ticket>
{
    public Tickets(XmlElement Xml)
	{
        foreach (XmlElement item in Xml)
        {
            Ticket t = new Ticket(item);
            base.Add(t);
            
        }
    }
}
public class Ticket
{
    #region Ticket Arguments
    
    /// <summary>
    /// The ticket ID
    /// </summary>
    public int ID { get; set; }
    public int EventId { get; set; }
    /// <summary>
    /// The ticket name
    /// </summary>
    public string Name { get; set; }
    /// <summary>
    /// The ticket price (not provided if the ticket is a donation)
    /// </summary>
    public float Price { get; set; }
    /// <summary>
    /// The number of tickets available. Not required for donations
    /// </summary>
    public int Quantity { get; set; }
    /// <summary>
    /// The ticket description.
    /// </summary>
    public string Description { get; set; }
    /// <summary>
    /// 0 for fixed-price tickets, 1 for donations
    /// </summary>
    public int Type { get; set; }
    /// <summary>
    /// The ticket currency, in 3-letter ISO 4217 format (e.g., "USD")
    /// </summary>
    public string Currency { get; set; }
    /// <summary>
    /// The date and time when ticket sales start, in ISO 8601 format (e.g., "2007-12-31 23:59:59")
    /// </summary>
    public string StartDate { get; set; }
    /// <summary>
    /// The date and time when ticket sales stop, in ISO 8601 format (e.g., "2007-12-31 23:59:59")
    /// </summary>
    public string EndDate { get; set; }
    /// <summary>
    /// Number of tickets for sale (requires authentication)
    /// </summary>
    public int QuantityAvailable { get; set; }
    /// <summary>
    /// Number of tickets sold (requires authentication)
    /// </summary>
    public int QuantitySold { get; set; }
    /// <summary>
    /// Flag indicating if the ticket is visible on the event registration page
    /// </summary>
    public bool Visible { get; set; }

    #endregion
    internal Ticket(XmlElement XmlElement) { CreateTicket(XmlElement); }
    public Ticket(XmlDocument XmlDocument) { CreateTicket(XmlDocument.DocumentElement); }
    public Ticket(int EventId, string Name, float Price, int Quantity) { this.EventId = EventId; this.Name = Name; this.Price = Price; this.Quantity = Quantity; }

    private void CreateTicket(XmlElement XmlElement)
    {
        foreach (XmlElement item in XmlElement)
        {
            switch (item.Name)
            {
                case "id":
                    this.ID = Convert.ToInt32(item.InnerText);
                    break;
                case "name":
                    this.Name= item.InnerText;
                    break;
                case "description":
                    this.Description = item.InnerText;
                    break;
                case "type":
                    this.Type = Convert.ToInt32(item.InnerText);
                    break;
                case "currency":
                    this.Currency = item.InnerText;
                    break;
                case "price":
                    this.Price = float.Parse(item.InnerText);
                    break;
                case "start_date":
                    this.StartDate = item.InnerText;
                    break;
                case "end_date":
                    this.EndDate = item.InnerText;
                    break;
                case "quantity_available":
                    this.QuantityAvailable = Convert.ToInt32(item.InnerText);
                    break;
                case "quantity_sold":
                    this.QuantitySold = Convert.ToInt32(item.InnerText);
                    break;
                case "visible":
                    this.Visible = Convert.ToBoolean(item.InnerText);
                    break;
                default:
                    break;
            }
        }
    }
}
